/*
 * Copyright 2006-2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.krtrain.sports.tracker;

import java.util.ArrayList;
import java.util.List;

import org.krtrain.sports.player.Player;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.KRADConstants;

import edu.sampleu.SampleuConstants;

/**
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
public class GameUtils {

    public static void setupGame(Game game) {
        game.refreshReferenceObject("homeTeam");
        game.refreshReferenceObject("visitingTeam");

        // create a player statistic for each player
        List<PlayerStatistics> playerStatistics = new ArrayList<PlayerStatistics>();
        for (Player player : game.getHomeTeam().getPlayers()) {
            PlayerStatistics stats = new PlayerStatistics();
            stats.setTeamName(game.getHomeTeamName());
            stats.setPlayerNumber(player.getNumber());
            stats.setPlayer(player);

            playerStatistics.add(stats);
        }

        for (Player player : game.getVisitingTeam().getPlayers()) {
            PlayerStatistics stats = new PlayerStatistics();
            stats.setTeamName(game.getVisitingTeamName());
            stats.setPlayerNumber(player.getNumber());
            stats.setPlayer(player);

            playerStatistics.add(stats);
        }
        game.setPlayerStats(playerStatistics);

        // add game created msg
        GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES,
                SampleuConstants.KeyConstants.GAME_CREATED_MSG);
    }

    public static void computeTotals(Game game, TeamStatistics teamStatistics, int teamIndicator) {
        int fieldGoals = 0;
        int threePointers = 0;
        int freeThrows = 0;
        int shotsTaken = 0;
        int rebounds = 0;
        int blocks = 0;
        int assists = 0;
        int steals = 0;
        int totalPoints = 0;

        teamStatistics.setPlayerStats(new ArrayList<PlayerStatistics>());

        for (PlayerStatistics playerStatistic : game.getPlayerStats()) {
            if ((teamIndicator == 1) && playerStatistic.getTeamName().equals(game.getHomeTeamName())
                    || (teamIndicator == 2) && playerStatistic.getTeamName().equals(game.getVisitingTeamName())) {
                fieldGoals += playerStatistic.getFieldGoals();
                threePointers += playerStatistic.getThreePointers();
                freeThrows += playerStatistic.getFreeThrows();
                shotsTaken += playerStatistic.getShotsTaken();
                rebounds += playerStatistic.getRebounds();
                blocks += playerStatistic.getBlocks();
                assists += playerStatistic.getAssists();
                steals += playerStatistic.getSteals();
                totalPoints += playerStatistic.getTotalPoints();
                teamStatistics.getPlayerStats().add(playerStatistic);
            }
        }

        teamStatistics.setAssists(assists);
        teamStatistics.setBlocks(blocks);
        teamStatistics.setFieldGoals(fieldGoals);
        teamStatistics.setFreeThrows(freeThrows);
        teamStatistics.setRebounds(rebounds);
        teamStatistics.setShotsTaken(shotsTaken);
        teamStatistics.setSteals(steals);
        teamStatistics.setThreePointers(threePointers);
        teamStatistics.setTotalPoints(totalPoints);
    }

    public static String buildGamePlayDescription(Player player, String property, int minutes, int seconds, int half) {
        String playDescription = minutes + ":" + seconds;

        if (half == 1) {
            playDescription += " 1st Half";
        } else {
            playDescription += " 2nd Half";
        }

        playDescription += " - " + player.getLastName() + " for " + player.getTeamName() + " makes a ";

        if ("fieldGoals".equals(property)) {
            playDescription += "field goal";
        } else if ("threePointers".equals(property)) {
            playDescription += "three pointer";
        } else if ("freeThrows".equals(property)) {
            playDescription += "free throw";
        } else if ("threePointers".equals(property)) {
            playDescription += "three pointer";
        } else if ("rebounds".equals(property)) {
            playDescription += "rebound";
        } else if ("blocks".equals(property)) {
            playDescription += "block";
        } else if ("assists".equals(property)) {
            playDescription += "assist";
        } else if ("steals".equals(property)) {
            playDescription += "steal";
        }

        return playDescription;
    }
}
